package com.web.servlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/verifyImg")
public class verifyServet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //response.setContentType("text/html;charset=utf-8");
        //生成验证码
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);
        //验证码图片写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        //将正确的验证码字符串，放入session中，会跟用户输入的验证码做对比
        String verifyCode = lineCaptcha.getCode();
        System.out.println(verifyCode);
        //把验证码放到Session
        request.getSession().setAttribute("verifyCode",verifyCode);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
